package com.sky.service.impl;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCarMapper;
import com.sky.properties.JwtProperties;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCarService;
import com.sky.utils.JwtUtil;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCarServiceImpl implements ShoppingCarService {

    private final Integer DEFAULT_NUMBER = 1;

    private final int INCREAM = 1;

    @Autowired
    private ShoppingCarMapper shoppingCarMapper;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;


    public void insert(ShoppingCart shoppingCart) {
        List<ShoppingCart> list = shoppingCarMapper.getShoppingCartList(shoppingCart);
        if (list != null && list.size() > 0) {
            Long count = (long) (list.get(0).getNumber() + INCREAM);
            shoppingCart.setNumber(Math.toIntExact(count));
            BigDecimal amount = calculateAmountAndPrice(count,shoppingCart);
            shoppingCart.setAmount(amount);
            shoppingCarMapper.update(shoppingCart);
        }
        else{
            if (shoppingCart.getDishId() != null && shoppingCart.getDishId() > 0) {
                DishVO dishById = dishService.getDishById(Math.toIntExact(shoppingCart.getDishId()));
                shoppingCart.setImage(dishById.getImage());
                shoppingCart.setAmount(dishById.getPrice());
                shoppingCart.setName(dishById.getName());
                shoppingCart.setDishFlavor(shoppingCart.getDishFlavor());
            }else {
                SetmealVO setmealById = setmealService.getSetmealById(Math.toIntExact(shoppingCart.getSetmealId()));
                shoppingCart.setAmount(setmealById.getPrice());
                shoppingCart.setImage(setmealById.getImage());
                shoppingCart.setName(setmealById.getName());
            }
            shoppingCart.setNumber(DEFAULT_NUMBER);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCarMapper.insert(shoppingCart);
        }
    }


    public List<ShoppingCart> getShoppingCartList(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        return shoppingCarMapper.getShoppingCartList(shoppingCart);
    }

    public void cleanShoppingCart(Long userId) {
        shoppingCarMapper.delet(userId);
    }


    public void subShoppingCart(ShoppingCart shoppingCart) {
        List<ShoppingCart> list = shoppingCarMapper.getShoppingCartList(shoppingCart);
        if (list != null && list.size() > 0){
            if (list.get(0).getNumber() == INCREAM){
                shoppingCarMapper.delete(shoppingCart);
                return;
            }
            Long count = (long) (list.get(0).getNumber() - INCREAM);
            shoppingCart.setNumber(Math.toIntExact(count));
            BigDecimal amount = calculateAmountAndPrice(count,shoppingCart);
            shoppingCart.setAmount(amount);
            shoppingCarMapper.update(shoppingCart);
        }
    }

    public List<ShoppingCart> list(Long userId) {
        return shoppingCarMapper.list(userId);
    }


    private BigDecimal calculateAmountAndPrice(Long count,ShoppingCart shoppingCart) {
            BigDecimal amount = null;
        if (shoppingCart.getSetmealId() != null && shoppingCart.getSetmealId() > 0) {
            SetmealVO setmealById = setmealMapper.getSetmealById(Math.toIntExact(shoppingCart.getSetmealId()));
            amount = setmealById.getPrice().multiply(BigDecimal.valueOf(count));
        }
        if (shoppingCart.getDishId() != null && shoppingCart.getDishId() > 0) {
            DishVO dishById = dishMapper.getDishById(Math.toIntExact(shoppingCart.getDishId()));
            amount = dishById.getPrice().multiply(BigDecimal.valueOf(count));
        }
        return amount;
    };

    private void insertOrderDeatil(OrderDetail orderDetail){

    }
}
